/**
 * Copyright (c) Jan 28, 2013 5:07:11 PM PM WKM and/or its affiliates. 
 * Use is subject to license terms.
 * All rights reserved.
 */
package org.extblog.wkm.web.dao.face;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.extblog.wkm.web.dao.AbstractDao;
import org.extblog.wkm.web.util.PropertyUtil;
import org.extblog.wkm.web.vo.BodyItemFaceView;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

/**
 * @author WKM
 * BodyDao.java history
 * 1.WKM - Jan 21, 2013 - Create File
 */
@Component
public class BodyDao extends AbstractDao {

	/**
	 * 添加 body
	 * @param view
	 * @return
	 */
	public int insertBody(BodyItemFaceView view){
		log.info("insert:" + PropertyUtil.getValue("bodys.insert.body"));
		return getTemplate().update(PropertyUtil.getValue("bodys.insert.body"),
					view.getItemId(),
					view.getItemParent(),
					view.getItemName(),
					view.getTarget(),
					view.getItemSeq(),
					view.getItemDesc());
	}
	
	public List<Map<String, String>> selectBodys(BodyItemFaceView view){
		log.info("query:" + PropertyUtil.getValue("bodys.select.children.byparentid"));
		return getTemplate().query(PropertyUtil.getValue("bodys.select.children.byparentid"), new Mapper(),view.getItemParent());
	}
	
	public int updateBody(BodyItemFaceView view){
		log.info("update:" + PropertyUtil.getValue("bodys.update.body"));
		return getTemplate().update(PropertyUtil.getValue("bodys.update.body"),
				view.getItemName(),
				view.getTarget(),
				view.getItemDesc(),
				view.getItemId());
	}
	
	public int deleteBody(BodyItemFaceView view){
		log.info("delete:" + PropertyUtil.getValue("bodys.delete.body"));
		return getTemplate().update(PropertyUtil.getValue("bodys.delete.body"),view.getItemId());
	}
	
	
	/**
	 * 数据单条记录使用map记录
	 * @author WKM
	 * FaceDao.java history
	 * 1.WKM - Jan 16, 2013 - Create File
	 */
	private class Mapper implements RowMapper<Map<String, String>>{

		public Map<String, String> mapRow(ResultSet rs, int rowNum)
				throws SQLException {
			// TODO Auto-generated method stub
			Map<String, String> map = new HashMap<String, String>();
			String itemId = rs.getString("url_id");
			if(itemId != null){
				map.put("itemId", itemId);
			}
			
			String itemParent = rs.getString("url_parent");
			if(itemParent != null){
				map.put("itemParent", itemParent);
			}
			
			String itemName = rs.getString("url_name");
			if(itemName != null){
				map.put("itemName", itemName);
			}
			
			String target = rs.getString("url_add");
			if(target != null){
				map.put("target", target);
			}
			String itemSeq = rs.getString("url_seq");
			if(itemSeq != null){
				map.put("itemSeq", itemSeq);
			}
			
			String itemDesc = rs.getString("url_desc");
			if(itemDesc != null){
				map.put("itemDesc", itemDesc);
			}
			return map;
		}
	}
}
